<?php



class Profile extends CI_Controller {
    function __construct(){
        parent::__construct();
	    $this->load->library(array('auth','form_validation'));
	    $this->load->helper('url');
	    if (!$this->auth->is_Login()) {
		    redirect("user/login");
		    exit();
	    }
        $this->load->helper(array("url",'my_data_helper'));
	    $this->load->model('user_model');
    }
    function index() {
        $this->load->view('logged_in/profile/index');
    }

	function update_avatar() {
		$data['update_avatar'] = '';
		$img = "";
		$flag = TRUE;
		if($_FILES['image']['name'] != NULL){
			//xoa avatar cu
			$img = 'images/avatar/'.$this->user_model->getInfo($this->session->userdata('userid'))['image'];
			if(file_exists($img))
				unlink($img);
			//upload file moi
			$config['upload_path']   = './images/avatar/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['encrypt_name']  = true; // ma hoa ten file
			$config['remove_spaces'] = true; // xoa khoang trang
			$this->load->library('upload', $config);

			if(!$this->upload->do_upload("image"))
			{
				$data['update_avatar'] = $this->upload->display_errors();
				$this->load->view("profile/index",$data);
				$flag = FALSE;
			}
			else
			{
				$img = $this->upload->data();
				$add['image'] = $img['file_name'];

				if($this->user_model->updateUser($add, $this->session->userdata('userid')))
					$data['update_avatar'] = "Your avatar change successful!";
				else $data['update_avatar'] = "Failed! Your avatar was not change successful.";

			}
		}
		$this->load->view("logged_in/profile/index",$data);

	}

	function update_info () {
		$add = array(
			"name" => $this->input->post("name"),
			"address"   => $this->input->post("address"),
			"gender"    => $_POST['gender']
		);
		if($this->user_model->updateUser($add, $this->session->userdata('userid')))
			$this->load->view("logged_in/profile/report",array('report'=>'Update info successful!'));

		else
			$this->load->view("logged_in/profile/index", array('update_info'=>'Failed! Update info not successful!'));
	}


	function change_email() {
		if(isset($_GET['key']) && isset($_GET['email']) && isset($_GET['userid'])) {
			if($_GET['key'] == md5($this->user_model->getInfo($_GET['userid'])['salt'])) {
				$data['email'] = $_GET['email'];
				if(!$this->user_model->updateUser($data, $_GET['userid'])) {
					$this->load->view("logged_in/profile/report", array('report'=>'Error on update your email!'));
					exit();
				}
				$this->load->view("logged_in/profile/report", array('report'=>'Your email has been changed to ' .  $_GET['email']));
			}
			else
				$this->load->view("logged_in/profile/report", array('report'=>'Error on update your email!'));
		}
		else {
			$new_email = $this->input->post("email");   //new email
			$id = $this->session->userdata('userid');
			$old_email = $this->user_model->getInfo($id)['email'];
			if($new_email != $old_email) {  //send email about the changing of email
				//change salt
				$data['salt'] = create_random_string(5);
				if(!$this->user_model->updateUser($data, $id)) {
					$this->load->view("logged_in/profile/report", array('report'=>'Error on update your email!'));
					exit();
				}
				$link = base_url()."profile/change_email/?userid=".$id."&email=".$old_email."&key=".md5($data['salt']);
				$message  = "Your email has been changed to " . $new_email . "! Follow this link to confirm: <br/>";
				$message .= "Link : <a href=".$link.">".$link."</a><br/>";

				$this->load->library("email");
				$this->email->from('tmptmp268@gmail.com', 'YourBlog Email System');
				$this->email->to($data['user']['email']);

				$this->email->subject('Active your account!');
				$this->email->message($message);
				$this->email->send();
			}
			$this->load->view("logged_in/profile/report", array('report'=>'Check your email to confirm new email!'));
		}
	}

	function change_pass() {
		$this->form_validation->set_rules("oldpass","Old Password","required|max_length[25]|callback_checkUser");
		$this->form_validation->set_rules("password","Password","required|matches[repassword]");
		$old_pass = $this->user_model->getInfo($this->session->userdata('userid'));
		$pass = $this->input->post('password');
		if($this->form_validation->run()==FALSE ){
			$this->load->view("logged_in/profile/index",array("change_pass"=>validation_errors()));
		}
		else if($old_pass != $pass) {
			$this->load->view("logged_in/profile/index",array("change_pass"=>'The password you entered not correct!'));
		}
		else {
			if($this->user_model->updateUser(array('password'=>md5($pass)), $this->session->userdata('userid')))
				$this->load->view("logged_in/profile/report",array("report"=>'Your password has been changed!'));
			else
				$this->load->view("logged_in/profile/index",array("change_pass"=>'Error on changing your password!'));
		}
	}
} 